Chlorophyll Concentration#

Hide code cell source
import warnings
warnings.filterwarnings("ignore")
import os
import os.path as op
import sys

import pandas as pd
import numpy as np
import xarray as xr
import geopandas as gpd
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

sys.path.append("../../../../indicators_setup")
from ind_setup.plotting_int import plot_timeseries_interactive, plot_oni_index_th
from ind_setup.plotting import plot_base_map, plot_map_subplots, add_oni_cat, plot_bar_probs, fontsize

sys.path.append("../../../functions")
from data_downloaders import download_ERDDAP_data, download_oni_index

Setup#

Define area of interest

#Area of interest
lon_range  = [129.4088, 137.0541]
lat_range = [1.5214, 11.6587]

EEZ shapefile

shp_f = op.join(os.getcwd(), '..', '..','..', 'data/Palau_EEZ/pw_eez_pol_april2022.shp')
shp_eez = gpd.read_file(shp_f)

Download Data#

update_data = False
path_data = "../../../data"
path_figs = "../../../matrix_cc/figures"
Hide code cell source
base_url = 'https://oceanwatch.pifsc.noaa.gov/erddap/griddap/esa-cci-chla-monthly-v6-0.csv'
dataset_id = 'chlor_a'
if update_data:
    date_ini = '1998-01-01T00:00:00Z'
    date_end = '2023-12-01T00:00:00Z'
    data = download_ERDDAP_data(base_url, dataset_id, date_ini, date_end, lon_range, lat_range)
    data_xr = data.set_index(['latitude', 'longitude', 'time']).to_xarray()
    data_xr['time'] = pd.to_datetime(data_xr.time)
    data_xr = data_xr.coarsen(longitude=2, latitude=2, boundary = 'pad').mean()
    data_xr.to_netcdf(op.join(path_data, f'griddap_{dataset_id}.nc'))
else:
    data_xr = xr.open_dataset(op.join(path_data, f'griddap_{dataset_id}.nc'))

Analysis#

Average#

ax = plot_base_map(shp_eez = shp_eez, figsize = [10, 6])
im = ax.pcolor(data_xr.longitude, data_xr.latitude, data_xr.mean(dim='time')[dataset_id], transform=ccrs.PlateCarree(), 
                cmap = 'Greens', vmin = np.percentile(data_xr.mean(dim = 'time')[dataset_id], 1), 
                vmax = np.percentile(data_xr.mean(dim = 'time')[dataset_id], 99))
ax.set_extent([lon_range[0], lon_range[1], lat_range[0], lat_range[1]], crs=ccrs.PlateCarree())
plt.colorbar(im, ax=ax, label='chlorophyll (mg/$m^3$)')
plt.savefig(op.join(path_figs, 'F15_chlorophyll_mean_map.png'), dpi=300, bbox_inches='tight')
../../../_images/6623d07308ec54745540200de935acb2af183bd3d0c18ad3a7ec04bd04baea67.png

Annual average#

data_y = data_xr.resample(time='1YE').mean()
fig = plot_map_subplots(data_y, dataset_id, shp_eez = shp_eez, cmap = 'Greens', vmin = np.percentile(data_xr.mean(dim = 'time')[dataset_id], 1), 
                vmax = np.percentile(data_xr.mean(dim = 'time')[dataset_id], 99), cbar = 1)
../../../_images/f161c01993d379ab795c1d85da52355d2dcafb42a8cd3e54269ff37892514913.png

Annual anomaly#

data_an = data_y - data_xr.mean(dim='time')
fig = plot_map_subplots(data_an, dataset_id, shp_eez = shp_eez, cmap='RdBu_r', vmin=-.1, vmax=.1, cbar = 1)
../../../_images/8ba0a0b93e44f6470eeb3d8c3653ea1c0f3893fe4e4428e62a728f7d4591587c.png

Average over area#

dict_plot = [{'data' : data_xr.mean(dim = ['longitude', 'latitude']).to_dataframe(), 
              'var' : dataset_id, 'ax' : 1, 'label' : 'Chlorophyll - MEAN AREA'},]
fig = plot_timeseries_interactive(dict_plot, trendline=True, scatter_dict = None, figsize = (25, 12), label_yaxes = 'Chlorophyll (mg/m3)');
fig.write_html(op.join(path_figs, 'F15_chlorophyll_mean_trend.html'), include_plotlyjs="cdn")

Top 10 years#

data_mean = data_xr.mean(dim = ['longitude', 'latitude']).to_dataframe()
data_mean = data_mean.resample('YE').mean()
top_10 = data_mean.sort_values(by='chlor_a', ascending=False).head(10)

fig, ax, trend = plot_bar_probs(x=data_mean.index.year, y=data_mean.chlor_a, trendline=True,
                                y_label='Chlorophyll-a', figsize=[15, 4], return_trend=True)
ax.set_ylim(data_mean.chlor_a.min(), data_mean.chlor_a.max()+.01)

im = ax.scatter(top_10.index.year, top_10.chlor_a, 
                c=top_10.chlor_a.values, s=100, ec = 'pink', cmap='rainbow', label='Top 10 warmest years')
plt.colorbar(im, pad = 0, shrink = .7).set_label('Absolute Chlorophyll-a', fontsize=fontsize)
ax.set_title('Annual Mean Chlorophyll-a anomalies', fontsize=15);
../../../_images/4875795b99c13c4f077a00e507cf7d66fd9424121c2590e39b76c5878d1a3546.png
d_p = data_xr.mean(dim = ['longitude', 'latitude']).to_dataframe()

Timeseries at a given point#

loc = [7.37, 134.7]
dict_plot = [{'data' : data_xr.sel(longitude=loc[1], latitude=loc[0], method='nearest').to_dataframe(), 
              'var' : dataset_id, 'ax' : 1, 'label' : f'Chlorophyll at [{loc[0]}, {loc[1]}]'},]
ax = plot_base_map(shp_eez = shp_eez, figsize = [10, 6])
ax.set_extent([lon_range[0], lon_range[1], lat_range[0], lat_range[1]], crs=ccrs.PlateCarree())
ax.plot(loc[1], loc[0], '*', markersize = 12, color = 'royalblue', transform=ccrs.PlateCarree(), label = 'Location Analysis')
ax.legend()
<matplotlib.legend.Legend at 0x17ad41ee0>
../../../_images/038a2757803511e8b284fa11a57d0ba084269dbabe56e09215299e69572e231d.png
fig = plot_timeseries_interactive(dict_plot, trendline=True, scatter_dict = None, figsize = (25, 12), label_yaxes = 'Chlorophyll (mg/m3)');

ONI index analysis#

p_data = 'https://psl.noaa.gov/data/correlation/oni.data'
df1 = download_oni_index(p_data)
lims = [-.5, .5]
plot_oni_index_th(df1, lims = lims)
df1 = add_oni_cat(df1, lims = lims)
df1['ONI'] = df1['oni_cat']
data_xr['ONI'] = (('time'), df1.iloc[np.intersect1d(data_xr.time, df1.index, return_indices=True)[2]].ONI.values)
data_xr['ONI_cat'] = (('time'), np.where(data_xr.ONI < lims[0], -1, np.where(data_xr.ONI > lims[1], 1, 0)))
data_oni = data_xr.groupby('ONI_cat').mean()

Average#

fig = plot_map_subplots(data_oni, dataset_id, shp_eez = shp_eez, cmap = 'Greens', 
                  vmin = np.percentile(data_xr.mean(dim = 'time')[dataset_id], 1), 
                  vmax = np.percentile(data_xr.mean(dim = 'time')[dataset_id], 99),
                  sub_plot= [1, 3], figsize = (20, 9),  cbar = True, cbar_pad = 0.1,
                  titles = ['La Niña', 'Neutral', 'El Niño'],)
plt.savefig(op.join(path_figs, 'F15_chlorophyll_ENSO.png'), dpi=300, bbox_inches='tight')
../../../_images/469329ca5f41a9236491b238b2fd7c064f3263dbec321d85a4a5b771759ddc3e.png

Anomaly#

data_an = data_oni - data_xr.mean(dim='time')
fig = plot_map_subplots(data_an, dataset_id, shp_eez = shp_eez, cmap='RdBu_r', vmin=-.05, vmax=.05,
                  sub_plot= [1, 3], figsize = (20, 9),  cbar = True, cbar_pad = 0.1,
                  titles = ['La Niña', 'Neutral', 'El Niño'],)
../../../_images/332e565096fd6cf11e028befc573cd54041d25300e89d68de084de3623db6fac.png